package io.framework.modules.water.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.modules.station.dto.StationDTO;
import io.framework.modules.station.service.StationService;
import io.framework.modules.water.dao.OceanBuoyPointDao;
import io.framework.modules.water.dto.OceanBuoyPointDTO;
import io.framework.modules.water.entity.OceanBuoyPointEntity;
import io.framework.modules.water.service.OceanBuoyPointService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 海洋浮标点信息
 *
 * @author Gujq email
 * @since 1.0.0 2020-07-21
 */
@Service
public class OceanBuoyPointServiceImpl extends CrudServiceImpl<OceanBuoyPointDao, OceanBuoyPointEntity, OceanBuoyPointDTO> implements OceanBuoyPointService {

    @Autowired
    private StationService stationService;

    @Override
    public QueryWrapper<OceanBuoyPointEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<OceanBuoyPointEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public Boolean checkData(String stationCode, String month) {
        Integer count = baseDao.getOceanInfoList(stationCode, month);
        return count > 0 ? true : false;
    }

    @Override
    public Boolean deleteBatchByDate(String stationCode, List<Date> list) {
        boolean flag = false;
        if (baseDao.deleteBatchByDate(stationCode, list) > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Map<String, Object>> getOceanInfoListByParams(Map<String, Object> params) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        String stationCode[] = params.get("stationCode").toString().split(",");
        for (String code : stationCode) {
            //查询站点名称
            Map<String, Object> staMap = new HashMap<>(1);
            staMap.put("code", code);
            List<StationDTO> stationDTOList = stationService.getStationInfoByParams(staMap);

            Map<String, Object> map = new HashMap<>(2);
            params.put("stationCode", code);
            map.put("listData", baseDao.getOceanInfoListByParams(params));
            map.put("stationName", stationDTOList.get(0).getName());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> getOceanInfoLabel(Map<String, Object> params) {

        String stationCode[] = params.get("stationCode").toString().split(",");

        return baseDao.getOceanInfoLabel(stationCode, stationCode.length);
    }

    @Override
    public OceanBuoyPointDTO getOceanInfoByStationCode(String stationCode) {

        OceanBuoyPointEntity entity = baseDao.getOceanInfoByStationCode(stationCode);

        return ConvertUtils.sourceToTarget(entity, OceanBuoyPointDTO.class);
    }

    @Override
    public PageData<OceanBuoyPointDTO> pageList(Map<String, Object> params) {
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
        String orderField = jsonObject.getString("orderField");
        String order = jsonObject.getString("order");
        String time = jsonObject.getString("time");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        QueryWrapper<OceanBuoyPointEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(time)) {
            queryWrapper.eq("date_format(date,'%Y-%m')", time);
        }
        Page<OceanBuoyPointEntity> p = new Page<>(page, limit);
        if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) {
            if (order.equals("desc")) {
                queryWrapper.orderByDesc(orderField);
            } else {
                queryWrapper.orderByAsc(orderField);
            }
        }
        IPage<OceanBuoyPointEntity> ip = baseDao.selectPage(p, queryWrapper);
        List<OceanBuoyPointDTO> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), OceanBuoyPointDTO.class);
        return new PageData<OceanBuoyPointDTO>(dtoList, ip.getTotal());
    }
}
